<!-- 基础设置--点位管理页面 -->
<template>
  <div>
    <div v-show="isMain" class="equip-wrap-common">
      <px-form class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
        <px-row :gutter="16">
          <px-col :span="6">
            <px-form-item label="点位编码">
              <px-input
                v-model="queryParams.tagCode"
                clearable
                placeholder="请输入"
                maxlength="50"
                @blur="queryParams.tagCode = $event.target.value.trim()"
                @keyup.enter="queryTable"
              >
                <template #suffix>
                  <px-icon style="cursor: pointer" @click="queryTable">
                    <search />
                  </px-icon>
                </template>
              </px-input>
            </px-form-item>
          </px-col>
          <px-col :span="6">
            <px-form-item label="设备编码">
              <px-input
                v-model="queryParams.eqpId"
                clearable
                placeholder="请输入"
                maxlength="50"
                @blur="queryParams.eqpId = $event.target.value.trim()"
                @keyup.enter="queryTable"
              >
                <template #suffix>
                  <px-icon style="cursor: pointer" @click="queryTable">
                    <search />
                  </px-icon>
                </template>
              </px-input>
            </px-form-item>
          </px-col>
          <px-col :span="6">
            <!-- 常用点位 -->
            <px-form-item label="常量点位">
              <px-select v-model="queryParams.isConstant" clearable filterable placeholder="请选择">
                <px-option v-for="(item, index) in isConstantList" :key="index" :label="item.label" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="6">
            <!-- 子包 -->
            <px-form-item label="子包">
              <px-select v-model="queryParams.intouchPackageName" filterable clearable placeholder="请选择" style="width: 100%">
                <px-option v-for="item in intouchPackageNameList" :key="item.id" :label="item.name" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="6">
            <!-- //科室 -->
            <EquipClassSelect :className="queryParams.className" @changeClassName="changeClassName" />
          </px-col>
          <px-col :span="6">
            <!-- 系统 -->
            <EquipSystemTree
              :className="queryParams.className"
              :systemCode="queryParams.systemCode"
              @changeAllSystemCodes="changeAllSystemCodes"
              @changeSystemCodes="changeSystemCodes"
              @clearSystem="clearSystem"
            />
          </px-col>
          <px-col :span="6">
            <px-form-item label="是否绑定设备编码">
              <px-select v-model="queryParams.isEqpId" filterable clearable placeholder="请选择">
                <px-option v-for="(item, index) in isEqpIdList" :key="index" :label="item.label" :value="item.value" />
              </px-select>
            </px-form-item>
          </px-col>
          <px-col :span="6">
            <px-button type="primary" @click="queryTable">查询</px-button>
            <px-button @click="resetTable">重置</px-button>
          </px-col>
        </px-row>
      </px-form>
      <div class="equip-table-btn">
        <px-button v-has="'SYSTEM_BASE_POINT_MANAGE_ADD'" icon="Plus" style="margin-right: 8px" type="primary" @click="addSpareParts">新增</px-button>
        <px-button v-has="'SYSTEM_BASE_POINT_MANAGE_EXPORT'" icon="Upload" plain style="margin-right: 8px" type="primary" @click="handleExport">
          导出
        </px-button>
        <px-button
          v-has="'SYSTEM_BASE_POINT_MANAGE_DOWNLOAD'"
          icon="Download"
          plain
          style="margin-right: 8px"
          type="primary"
          @click="templateDownload"
        >
          下载模板
        </px-button>
        <px-upload
          v-has="'SYSTEM_BASE_POINT_MANAGE_BATCH_UPLOAD'"
          action="#"
          :http-request="handleImport"
          :show-file-list="false"
          accept=".xlsx, xls"
        >
          <px-button type="primary" icon="Download">批量导入</px-button>
        </px-upload>
      </div>
      <px-table :data="tableData" class="equip-table" size="small" height="calc(100vh - 346px)" border @selection-change="selectionChange">
        <px-table-column type="selection" min-width="40" align="center" />
        <px-table-column type="index" label="序号" width="60" align="center" fixed="left" />
        <px-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :label="item.label"
          :min-width="item.minWidth"
          :align="item.align"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template #default="scope">{{
            scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
          }}</template>
        </px-table-column>
        <px-table-column fixed="right" label="操作" min-width="100" align="center">
          <template #default="scope">
            <px-tooltip content="详情" placement="top">
              <px-button v-has="'SYSTEM_BASE_POINT_MANAGE_VIEW'" link type="primary" icon="Tickets" @click="handleView(scope.row)" />
            </px-tooltip>
            <px-tooltip content="编辑" placement="top">
              <px-button v-has="'SYSTEM_BASE_POINT_MANAGE_EDIT'" link type="primary" icon="EditPen" @click="handleEdit(scope.row)" />
            </px-tooltip>
            <!-- <px-tooltip content="删除" placement="top">
              <px-button v-has="'ALL_EQUIP_SPAREPARTS_LEDGER_DELETE'" link type="danger" icon="Delete" @click="handleDelete(scope.row)" />
            </px-tooltip> -->
          </template>
        </px-table-column>
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        class="equip-table-pagination"
        :total="total"
        @pagination="queryTable"
      />
    </div>
    <!-- 详情新增/编辑页面 -->
    <AddPointManageDetail
      :isMain="isMain"
      :isView="isView"
      :drawerType="drawerType"
      :rowData="rowData"
      :username="username"
      @queryTable="queryTable"
      @backMainPage="backMainPage"
    />
    <!-- 详情查看页面 -->
    <ViewPointManageDetail :isMain="isMain" :isView="isView" :rowData="rowData" @backMainPage="backMainPage" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SYSTEM_BASE_POINT_MANAGE"
});
import { queryDictData } from "@/api/common";
import { getPointImport, pagePointList } from "@/api/system/pointManage";
import { useNav } from "@/layout/hooks/useNav";
import EquipClassSelect from "@/views/all/equip/components/EquipClassSelect.vue";
import EquipSystemTree from "@/views/all/equip/components/EquipSystemTree.vue";
import { PxMessage } from "px-ui";
import { getCurrentInstance, onMounted, reactive, toRefs } from "vue";
import AddPointManageDetail from "./components/AddPointManageDetail.vue";
import ViewPointManageDetail from "./components/ViewPointManageDetail.vue";

const { proxy }: any = getCurrentInstance();

const { username } = useNav();
const state = reactive<any>({
  isMain: true, // 主页面true 详情页面false
  isView: true, // 查看true 新增/编辑false
  intouchPackageNameList: [],
  isConstantList: [
    {
      value: 1,
      label: "是"
    },
    {
      value: 0,
      label: "否"
    }
  ],
  isEqpIdList: [
    {
      value: 1,
      label: "是"
    },
    {
      value: 0,
      label: "否"
    }
  ],
  queryParams: {
    tagCode: "",
    eqpId: "",
    isConstant: "",
    intouchPackageName: "",
    className: "",
    systemCode: "",
    isEqpId: "",
    pageNum: 1,
    pageSize: 20
  },
  tableColumns: [
    { prop: "tagCode", label: "点位编码", minWidth: "120", showOverflowTooltip: true, fixed: "left" },
    { prop: "tagComment", label: "点位描述", minWidth: "120", showOverflowTooltip: true },
    { prop: "tagUnitDesc", label: "点位单位", minWidth: "80", showOverflowTooltip: true },
    { prop: "dataType", label: "数据类型", minWidth: "80", showOverflowTooltip: true },
    { prop: "isConstantDesc", label: "是否常量点位", minWidth: "120", showOverflowTooltip: true },
    { prop: "constantValue", label: "常量值", minWidth: "100", showOverflowTooltip: true },
    { prop: "eqpId", label: "设备编码", minWidth: "140", showOverflowTooltip: true },
    { prop: "intouchPackageName", label: "子包", minWidth: "100", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "80", showOverflowTooltip: true },
    { prop: "systemCodeDesc", label: "系统", minWidth: "120", showOverflowTooltip: true },
    { prop: "positionNameAll", label: "物理位置信息", minWidth: "140", showOverflowTooltip: true },
    { prop: "createByName", label: "创建人", minWidth: "100", showOverflowTooltip: true },
    { prop: "createTime", label: "创建时间", minWidth: "150", showOverflowTooltip: true },
    { prop: "updateByName", label: "修改人", minWidth: "100", showOverflowTooltip: true },
    { prop: "updateTime", label: "修改时间", minWidth: "150", showOverflowTooltip: true }
  ],
  tableData: [],
  total: 0,
  selectIds: [],
  drawerType: "add",
  rowData: {}
});
const {
  isMain,
  isView,
  intouchPackageNameList,
  isConstantList,
  isEqpIdList,
  queryParams,
  tableColumns,
  tableData,
  total,
  selectIds,
  drawerType,
  rowData
} = toRefs(state);

// 进入页面时触发
onMounted(() => {
  queryTable();
  queryTagUnitList();
});

//子包下拉框
const queryTagUnitList = async () => {
  const tempClassNameList1 = await queryDictData({ typeCode: "INTOUCH_PACKAGE_NAME" });
  intouchPackageNameList.value = tempClassNameList1 || [];
};
// 查询备件列表
const queryTable = async () => {
  const params = JSON.parse(JSON.stringify(queryParams.value));
  console.log(params);
  const data = await pagePointList(params);
  tableData.value = data?.data || [];
  total.value = data?.total;
};
// 重置筛选条件和备件列表
const resetTable = async () => {
  queryParams.value = {
    tagCode: "",
    eqpId: "",
    isConstant: "",
    intouchPackageName: "",
    className: "",
    systemCode: "",
    isEqpId: "",
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};

// 导出
const handleExport = () => {
  if (selectIds.value.length == 0) {
    PxMessage.warning("请选择点位后操作");
    return;
  }
  const params = {
    codes: selectIds.value
  };
  proxy.download("post", "/account/api/v1/point/exportPoint", params);
};

// 下载模版
const templateDownload = async () => {
  proxy.download("post", "/account/api/v1/point/importPointTemplate", {});
};

const handleImport = async (options: any) => {
  try {
    console.log("导入文件", options);
    const { file } = options;
    const formData = new FormData();
    formData.append("file", file);
    await getPointImport(formData);
    PxMessage.success("批量导入成功");
    queryTable();
  } catch (error) {
    console.log(error);
  }
};

// 表格多选框选中
const selectionChange = (val: any) => {
  selectIds.value = [];
  val.forEach((item: any) => {
    if (item.tagCode) {
      selectIds.value.push(item.tagCode);
    }
  });
};
// 查看备件详情
const handleView = (row: any) => {
  rowData.value = row;
  isMain.value = false;
  isView.value = true;
};
// 添加备件详情
const addSpareParts = () => {
  drawerType.value = "add";
  isMain.value = false;
  isView.value = false;
};
// 编辑备件详情
const handleEdit = (row: any) => {
  drawerType.value = "edit";
  rowData.value = row;
  isMain.value = false;
  isView.value = false;
};
// 从新增/编辑、详情返回主页面
const backMainPage = () => {
  isMain.value = true;
};

// 外部封装好的组件
// 系统组件调用父组件
const changeSystemCodes = (val, val2) => {
  queryParams.value.systemCode = val;
};
// 科室组件调用父组件
const changeClassName = val => {
  queryParams.value.className = val;
  queryParams.value.systemCode = "";
};
// 系统组件调用父组件
const changeAllSystemCodes = (val, val2, val3, val4) => {
  queryParams.value.className = val2;
  queryParams.value.systemCode = val3;
};
// 系统组件值改变触发
const clearSystem = val => {
  queryParams.value.systemCode = "";
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");
</style>
